//
//  DangQiViewModel.m
//  zhuchiren
//
//  Created by rainday on 15/2/8.
//  Copyright (c) 2015年 rainday. All rights reserved.
//

#import "DangQiViewModel.h"
#import <AFNetworking.h>
#import "IP.h"

@implementation DangQiViewModel


- (id)init{

    self = [super init];
    if (self) {
        _dateArray = [NSMutableArray array];
        _eventDic = [NSMutableDictionary dictionary];
    }
    return self;
}
- (void)fetchDataForUser:(NSString *)username{

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    id  token = [userDefaults objectForKey:@"token"];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@",token] forHTTPHeaderField:@"Authorization"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    NSDictionary *params;
    if (username != nil) {
        params = @{@"username":username};
    }
    
    NSString *url = [NSString stringWithFormat:@"%s/schedule",SERVER];
    
    [manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [_dateArray removeAllObjects];
        [_eventDic removeAllObjects];
        NSArray *array = [responseObject objectForKey:@"schedules"];
        for (id item in array) {
            [_dateArray addObject:[item objectForKey:@"day"]];
            [_eventDic setValue:item forKey:[item objectForKey:@"day"]];
            
        }
        
        self.hasLoaded = YES;
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];

}

- (BOOL)validateDate:(NSDate *)date{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat: @"yyyy-MM-dd"];
    return  [_dateArray containsObject:[dateFormatter stringFromDate:date]];
    

}

- (NSDictionary *)dicForDate:(NSDate *)date{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat: @"yyyy-MM-dd"];
    return  [_eventDic objectForKey:[dateFormatter stringFromDate:date]];
    

}

- (void)updateSchedule:(NSString *)pk note:(NSString *)note beginTime:(NSString *)beginTime endTime:(NSString *)endTime{


    if (pk ==nil) {
        
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        id  token = [userDefaults objectForKey:@"token"];
        
        NSDictionary *params = @{@"beginTime":beginTime,
                                 @"endTime":endTime,
                                 @"note":note};
        
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        
        [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@",token] forHTTPHeaderField:@"Authorization"];
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        
        NSString *url = [NSString stringWithFormat:@"%s/schedule",SERVER];
        
        
        __weak DangQiViewModel * weakSelf = self;
        [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            [weakSelf fetchDataForUser:nil];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
        }];
    }else{
    
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        id  token = [userDefaults objectForKey:@"token"];
        
        NSDictionary *params = @{@"beginTime":beginTime,
                                 @"endTime":endTime,
                                 @"note":note,
                                 @"pk":pk};
        
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        
        [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@",token] forHTTPHeaderField:@"Authorization"];
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        
        NSString *url = [NSString stringWithFormat:@"%s/schedule",SERVER];
        __weak DangQiViewModel * weakSelf = self;
        [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [weakSelf fetchDataForUser:nil];
            
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
        }];
    
    }
}

@end
